import argparse
import sys
from shutil import copyfile
import os


def main(args):
    filePath = args.image_path_file
    outputDir = args.output_dir
    output_dir_damage = args.output_dir_damage
    if not os.path.exists(outputDir):
        os.mkdir(outputDir)
    if not os.path.exists(output_dir_damage):
        os.mkdir(output_dir_damage)
    print(filePath, outputDir)
    with open(filePath, 'r', True, encoding='utf-8') as file:
        for line in file:
            if line.startswith('Unable to align'):
                source = line[line.index("\"")+1:line.index("\"",line.index("\"")+1)]
                target =  os.path.join(outputDir, os.path.split(source)[1])
                try:
                    copyfile(source, target)
                except IOError as e:
                    print("Unable to copy file. %s" % e)
                except:
                    print("Unexpected error:", sys.exc_info())
            else:
                source = line[0:line.index(":",line.index(":")+1)]
                target = os.path.join(output_dir_damage, os.path.split(source)[1])
                try:
                    copyfile(source, target)
                except IOError as e:
                    print("Unable to copy file. %s" % e)
                except:
                    print("Unexpected error:", sys.exc_info())




def parse_arguments(argv):
    parser = argparse.ArgumentParser()

    parser.add_argument('--image_path_file', type=str, help='Unaligned face images.',
                        default=r'E:\testDir\ZS_BYZ\20210414103818未识别_80px.txt')
    parser.add_argument('--output_dir', type=str, help='Test MTCNN output dir',
                        default=r'E:\testDir\ZS_BYZ\20210414103818_re')
    parser.add_argument('--output_dir_damage', type=str, help='Test MTCNN output dir',
                        default=r'E:\testDir\ZS_BYZ\20210414103818_damage')
    return parser.parse_args(argv)


if __name__ == '__main__':
    main(parse_arguments(sys.argv[1:]))
